<!DOCTYPE html>
<html lang=zh>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5" />
    <meta name="description" content="call 与 apply 共同点 都能够改变函数执行时的this指向，将一个对象的方法交给另一个对象来执行，并且是立即执行的 比如，A 对象有一个方法，而 B 对象因为某种原因，也需要用到同样的方法，那么这时候我们可以借用一下 A 对象的方法，既完成了需求，又减少了内存的占用。 调用 call 和 apply 的对象，必须是一个函数 Function，因为call和apply方法存在于Functi">
<meta property="og:type" content="article">
<meta property="og:title" content="call、apply、bind">
<meta property="og:url" content="https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/index.html">
<meta property="og:site_name" content="柯里的语法糖">
<meta property="og:description" content="call 与 apply 共同点 都能够改变函数执行时的this指向，将一个对象的方法交给另一个对象来执行，并且是立即执行的 比如，A 对象有一个方法，而 B 对象因为某种原因，也需要用到同样的方法，那么这时候我们可以借用一下 A 对象的方法，既完成了需求，又减少了内存的占用。 调用 call 和 apply 的对象，必须是一个函数 Function，因为call和apply方法存在于Functi">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-11-19T16:00:00.000Z">
<meta property="article:modified_time" content="2022-03-05T12:31:10.764Z">
<meta property="article:author" content="柯里的语法糖">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
    
    
      
        
          <link rel="shortcut icon" href="https://codertzm.oss-cn-chengdu.aliyuncs.com/favicon.ico">
        
      
      
        
          <link rel="icon" type="image/png" href="https://codertzm.oss-cn-chengdu.aliyuncs.com/favicon.ico" sizes="192x192">
        
      
      
        
          <link rel="apple-touch-icon" sizes="180x180" href="https://codertzm.oss-cn-chengdu.aliyuncs.com/favicon.ico">
        
      
    
    <!-- title -->
    <title>call、apply、bind</title>
    <!-- styles -->
    
<link rel="stylesheet" href="/css/style.css">

    <!-- persian styles -->
    
    <!-- rss -->
    
    
	<!-- mathjax -->
	
<meta name="generator" content="Hexo 6.0.0"></head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#" aria-label="Menu"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#" aria-label="Menu"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" aria-label="Top" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
        <!--
       --><li><a href="/">首页</a></li><!--
     --><!--
       --><li><a href="/tags/">标签</a></li><!--
     --><!--
       --><li><a href="/categories/">分类</a></li><!--
     --><!--
       --><li><a href="/archives/">归档</a></li><!--
     --><!--
       --><li><a target="_blank" rel="noopener" href="http://github.com/twitzz">项目</a></li><!--
     --><!--
       --><li><a href="/about/">关于</a></li><!--
     --><!--
       --><li><a href="/search/">搜索</a></li><!--
     -->
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" aria-label="上一篇" href="/2021/12/18/JavaScript/eventLoop/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" aria-label="下一篇" href="/2021/11/15/JavaScript/this/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" aria-label="返回顶部" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" aria-label="分享文章" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">上一篇</span>
      <span id="i-next" class="info" style="display:none;">下一篇</span>
      <span id="i-top" class="info" style="display:none;">返回顶部</span>
      <span id="i-share" class="info" style="display:none;">分享文章</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&text=call、apply、bind"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&title=call、apply、bind"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&is_video=false&description=call、apply、bind"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=call、apply、bind&body=Check out this article: https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&title=call、apply、bind"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&title=call、apply、bind"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&title=call、apply、bind"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&title=call、apply、bind"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&name=call、apply、bind&description="><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&t=call、apply、bind"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#call-%E4%B8%8E-apply-%E5%85%B1%E5%90%8C%E7%82%B9"><span class="toc-number">1.</span> <span class="toc-text">call 与 apply 共同点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#call-%E4%B8%8E-apply-%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-number">2.</span> <span class="toc-text">call 与 apply 的区别</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#call-%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-number">2.1.</span> <span class="toc-text">call 的写法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#apply-%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-number">2.2.</span> <span class="toc-text">apply 的写法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#call-%E4%B8%8E-apply%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">3.</span> <span class="toc-text">call 与 apply的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#bind-%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">4.</span> <span class="toc-text">bind 的使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#bind-%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-number">4.1.</span> <span class="toc-text">bind 的写法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%89%8B%E5%8A%A8%E5%AE%9E%E7%8E%B0call%E3%80%81apply%E3%80%81bind"><span class="toc-number">5.</span> <span class="toc-text">手动实现call、apply、bind</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#call%E6%96%B9%E6%B3%95"><span class="toc-number">5.1.</span> <span class="toc-text">call方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#apply%E6%96%B9%E6%B3%95"><span class="toc-number">5.2.</span> <span class="toc-text">apply方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#bind%E6%96%B9%E6%B3%95"><span class="toc-number">5.3.</span> <span class="toc-text">bind方法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">6.</span> <span class="toc-text">总结</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        call、apply、bind
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">柯里的语法糖</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2021-11-19T16:00:00.000Z" itemprop="datePublished">2021-11-20</time>
        
      
    </div>


      
    <div class="article-category">
        <i class="fas fa-archive"></i>
        <a class="category-link" href="/categories/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80/">前端基础</a>
    </div>


      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link-link" href="/tags/JavaScript/" rel="tag">JavaScript</a>
    </div>


    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h2 id="call-与-apply-共同点"><a href="#call-与-apply-共同点" class="headerlink" title="call 与 apply 共同点"></a>call 与 apply 共同点</h2><ul>
<li>都能够改变函数执行时的this指向，将一个对象的方法交给另一个对象来执行，并且是立即执行的</li>
<li>比如，A 对象有一个方法，而 B 对象因为某种原因，也需要用到同样的方法，那么这时候我们可以借用一下 A 对象的方法，既完成了需求，又减少了内存的占用。</li>
<li>调用 call 和 apply 的对象，必须是一个函数 Function，因为call和apply方法存在于Function.prototype上</li>
<li>call 和 apply 的第一个参数是一个对象。Function 的调用者，将会指向这个对象，如果不传，默认情况下是window</li>
</ul>
<h2 id="call-与-apply-的区别"><a href="#call-与-apply-的区别" class="headerlink" title="call 与 apply 的区别"></a>call 与 apply 的区别</h2><h3 id="call-的写法"><a href="#call-的写法" class="headerlink" title="call 的写法"></a>call 的写法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Function</span>.call(obj,[param1[,param2[,…[,paramN]]]])</span><br></pre></td></tr></table></figure>

<p><strong>注意：</strong></p>
<ul>
<li>调用call的对象，必须是给函数 Function</li>
<li>第二个参数开始，可以接收多个参数。每个参数会映射到相应位置的 Function 的参数上</li>
</ul>
<h3 id="apply-的写法"><a href="#apply-的写法" class="headerlink" title="apply 的写法"></a>apply 的写法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Function</span>.apply(obj[,argArray])</span><br></pre></td></tr></table></figure>

<p><strong>注意：</strong></p>
<ul>
<li>第二个参数必须是数组或者是类数组，它们会被转换成类数组，传入 Function 中，并且会映射到相应位置的 Function 的参数上，这也是 call 和 apply 之间，很重要的区别</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">fn.apply(obj, [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>])</span><br><span class="line"><span class="comment">// fn 接收到的参数是 1,2,3</span></span><br><span class="line"></span><br><span class="line">fn.apply(obj, &#123;</span><br><span class="line">    <span class="number">0</span>: <span class="number">1</span>,</span><br><span class="line">    <span class="number">1</span>: <span class="number">2</span>,</span><br><span class="line">    <span class="number">2</span>: <span class="number">3</span>,</span><br><span class="line">    <span class="attr">length</span>: <span class="number">3</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// fn 接收到的参数是 1,2,3</span></span><br></pre></td></tr></table></figure>

<h2 id="call-与-apply的使用"><a href="#call-与-apply的使用" class="headerlink" title="call 与 apply的使用"></a>call 与 apply的使用</h2><ol>
<li>对象的继承</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Animal</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.species = <span class="string">&quot;动物&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Cat</span>(<span class="params">name,color</span>)</span>&#123;</span><br><span class="line">  Animal.call(<span class="built_in">this</span>);</span><br><span class="line">  <span class="built_in">this</span>.name = name;</span><br><span class="line">  <span class="built_in">this</span>.age = age;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>Cat 通过 call 方法，继承了 Animal 的 species 属性</p>
</blockquote>
<ol start="2">
<li>借用方法</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> args = <span class="built_in">Array</span>.prototype.slice.call(<span class="built_in">arguments</span>);</span><br></pre></td></tr></table></figure>

<blockquote>
<p>这里将arguments 对象转换成数组，其他将arguments转换为数组的方法可以在这里找到：<a href="/2021/11/01/JavaScript/arrayLike">类数组</a></p>
</blockquote>
<p>因为Array.prototype上的slice方法，其实内部大概做了这样的事情，因此将其中的this指向arguments能够实现数组的转换</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Array</span>.prototype.slice = <span class="function"><span class="keyword">function</span>(<span class="params">start,end</span>)</span>&#123;</span><br><span class="line">    <span class="comment">// 没有考虑边界情况，只考虑实现思路</span></span><br><span class="line">    <span class="keyword">var</span> arr = <span class="built_in">this</span></span><br><span class="line">    <span class="keyword">var</span> newArr = []</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">var</span> i = start; i &lt; end; i++ )&#123;</span><br><span class="line">        newArr.push(arr[i])</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> newArr</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>获取数组最大最小值</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> max = <span class="built_in">Math</span>.max.call(<span class="literal">null</span>,arr);</span><br><span class="line"><span class="keyword">let</span> min = <span class="built_in">Math</span>.min.call(<span class="literal">null</span>,arr);</span><br></pre></td></tr></table></figure>

<h2 id="bind-的使用"><a href="#bind-的使用" class="headerlink" title="bind 的使用"></a>bind 的使用</h2><h3 id="bind-的写法"><a href="#bind-的写法" class="headerlink" title="bind 的写法"></a>bind 的写法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Function</span>.bind(thisArg,[, arg1[, arg2[, ...]]])</span><br></pre></td></tr></table></figure>

<p>bind 方法的返回值是函数，需要调用才会执行，而 call 和 apply 是立即调用</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">add</span> (<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> a + b;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">add.bind(<span class="literal">null</span>, <span class="number">5</span>, <span class="number">3</span>); <span class="comment">// 这时，并不会返回 8</span></span><br><span class="line">add.bind(<span class="literal">null</span>, <span class="number">5</span>, <span class="number">3</span>)(); <span class="comment">// 调用后，返回 8</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>注意：bind方法可以在绑定的时候传递函数的参数，也可以在函数调用的时候，将剩余的参数传递过去</p>
</blockquote>
<h2 id="手动实现call、apply、bind"><a href="#手动实现call、apply、bind" class="headerlink" title="手动实现call、apply、bind"></a>手动实现call、apply、bind</h2><blockquote>
<p>这里主要提供实现思路，没有做过多的边界处理</p>
</blockquote>
<h3 id="call方法"><a href="#call方法" class="headerlink" title="call方法"></a>call方法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Function</span>.prototype.mycall = <span class="function"><span class="keyword">function</span>(<span class="params">thisArg,...argArray</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// this是调用call方法的函数</span></span><br><span class="line">  <span class="keyword">var</span> fn = <span class="built_in">this</span></span><br><span class="line">  <span class="comment">// 判断传参是否是null、undefined</span></span><br><span class="line">  thisArg = (thisArg !== <span class="literal">undefined</span> &amp;&amp; thisArg !== <span class="literal">null</span>) ? thisArg : <span class="built_in">window</span></span><br><span class="line">  <span class="comment">// 将基本类型的数据转换为对象</span></span><br><span class="line">  thisArg = <span class="built_in">Object</span>(thisArg)</span><br><span class="line">  <span class="comment">// this的隐式绑定</span></span><br><span class="line">  thisArg.fn = fn</span><br><span class="line">  <span class="comment">// 拿到函数的返回值</span></span><br><span class="line">  <span class="keyword">var</span> result = thisArg.fn(...argArray)</span><br><span class="line">  <span class="comment">// 删除属性</span></span><br><span class="line">  <span class="keyword">delete</span> thisArg.fn</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> result</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="apply方法"><a href="#apply方法" class="headerlink" title="apply方法"></a>apply方法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Function</span>.prototype.myapply = <span class="function"><span class="keyword">function</span>(<span class="params">thisArg,argArray</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// this是调用apply方法的函数</span></span><br><span class="line">  <span class="keyword">var</span> fn = <span class="built_in">this</span></span><br><span class="line">  <span class="comment">// 判断传参是否是null、undefined</span></span><br><span class="line">  thisArg = (thisArg !== <span class="literal">undefined</span> &amp;&amp; thisArg !== <span class="literal">null</span>) ? thisArg : <span class="built_in">window</span></span><br><span class="line">  <span class="comment">// 判断argArray是否是数组</span></span><br><span class="line">  <span class="keyword">if</span>(!(argArray <span class="keyword">instanceof</span> <span class="built_in">Array</span>))&#123;</span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&quot;CreateListFromArrayLike called on non-object&quot;</span>)</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 将基本类型的数据转换为对象</span></span><br><span class="line">  thisArg = <span class="built_in">Object</span>(thisArg)</span><br><span class="line">  <span class="comment">// this的隐式绑定</span></span><br><span class="line">  thisArg.fn = fn</span><br><span class="line">  <span class="comment">// 拿到函数的返回值</span></span><br><span class="line">  <span class="keyword">var</span> result = thisArg.fn(...argArray)</span><br><span class="line">  <span class="comment">// 删除属性</span></span><br><span class="line">  <span class="keyword">delete</span> thisArg.fn</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> result</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="bind方法"><a href="#bind方法" class="headerlink" title="bind方法"></a>bind方法</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Function</span>.prototype.mybind = <span class="function"><span class="keyword">function</span> (<span class="params">thisArg, ...argArray</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// this是调用bind方法的函数</span></span><br><span class="line">  <span class="keyword">var</span> fn = <span class="built_in">this</span></span><br><span class="line">  <span class="comment">// 判断传参是否是null、undefined</span></span><br><span class="line">  thisArg = thisArg !== <span class="literal">undefined</span> &amp;&amp; thisArg !== <span class="literal">null</span> ? thisArg : <span class="built_in">window</span></span><br><span class="line">  <span class="comment">// 将基本类型的数据转换为对象</span></span><br><span class="line">  thisArg = <span class="built_in">Object</span>(thisArg)</span><br><span class="line">  <span class="comment">// 代理方法</span></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">proxyFn</span>(<span class="params">...restArgArray</span>) </span>&#123;</span><br><span class="line">    thisArg.fn = fn</span><br><span class="line">    <span class="comment">// 将所有传递的参数收集起来</span></span><br><span class="line">    <span class="keyword">var</span> finalArr = [...argArray, ...restArgArray]</span><br><span class="line">    <span class="comment">// 得到返回值</span></span><br><span class="line">    <span class="keyword">var</span> result = thisArg.fn(...finalArr)</span><br><span class="line">    <span class="comment">// 删除不需要的fn属性</span></span><br><span class="line">    <span class="keyword">delete</span> thisArg.fn</span><br><span class="line">    <span class="keyword">return</span> result</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> proxyFn</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>call 和 apply 的主要作用，是改变函数内部的this指向，并且是立即执行的。它们在参数上的写法略有区别。</p>
<p>bind 也能改变函数内部的this指向，它与 call 和 apply 不同的是，返回值是一个函数，并且需要稍后再调用一下，才会执行。</p>

  </div>
</article>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/categories/">分类</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a target="_blank" rel="noopener" href="http://github.com/twitzz">项目</a></li>
         
          <li><a href="/about/">关于</a></li>
         
          <li><a href="/search/">搜索</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#call-%E4%B8%8E-apply-%E5%85%B1%E5%90%8C%E7%82%B9"><span class="toc-number">1.</span> <span class="toc-text">call 与 apply 共同点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#call-%E4%B8%8E-apply-%E7%9A%84%E5%8C%BA%E5%88%AB"><span class="toc-number">2.</span> <span class="toc-text">call 与 apply 的区别</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#call-%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-number">2.1.</span> <span class="toc-text">call 的写法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#apply-%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-number">2.2.</span> <span class="toc-text">apply 的写法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#call-%E4%B8%8E-apply%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">3.</span> <span class="toc-text">call 与 apply的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#bind-%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-number">4.</span> <span class="toc-text">bind 的使用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#bind-%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-number">4.1.</span> <span class="toc-text">bind 的写法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%89%8B%E5%8A%A8%E5%AE%9E%E7%8E%B0call%E3%80%81apply%E3%80%81bind"><span class="toc-number">5.</span> <span class="toc-text">手动实现call、apply、bind</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#call%E6%96%B9%E6%B3%95"><span class="toc-number">5.1.</span> <span class="toc-text">call方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#apply%E6%96%B9%E6%B3%95"><span class="toc-number">5.2.</span> <span class="toc-text">apply方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#bind%E6%96%B9%E6%B3%95"><span class="toc-number">5.3.</span> <span class="toc-text">bind方法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">6.</span> <span class="toc-text">总结</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&text=call、apply、bind"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&title=call、apply、bind"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&is_video=false&description=call、apply、bind"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=call、apply、bind&body=Check out this article: https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&title=call、apply、bind"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&title=call、apply、bind"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&title=call、apply、bind"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&title=call、apply、bind"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&name=call、apply、bind&description="><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://danyuan30.gitee.io/2021/11/20/JavaScript/call-apply-bind/&t=call、apply、bind"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> 菜单</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> 目录</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> 分享</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> 返回顶部</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy;
    
    
    2021-2022
    柯里的语法糖
  </div>
  <div class="footer-right">
    <nav>
      <ul>
        <!--
       --><li><a href="/">首页</a></li><!--
     --><!--
       --><li><a href="/tags/">标签</a></li><!--
     --><!--
       --><li><a href="/categories/">分类</a></li><!--
     --><!--
       --><li><a href="/archives/">归档</a></li><!--
     --><!--
       --><li><a target="_blank" rel="noopener" href="http://github.com/twitzz">项目</a></li><!--
     --><!--
       --><li><a href="/about/">关于</a></li><!--
     --><!--
       --><li><a href="/search/">搜索</a></li><!--
     -->
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->



  <link rel="preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" crossorigin="anonymous" onload="this.onload=null;this.rel='stylesheet'"/>


    <!-- jquery -->
 
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" crossorigin="anonymous"></script> 




<!-- clipboard -->

  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.7/clipboard.min.js" crossorigin="anonymous"></script> 
  
  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"复制到粘贴板!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "复制成功!");
      e.clearSelection();
    })
  })
  </script>


<script src="/js/main.js"></script>

<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

<!-- Cloudflare Analytics -->

<!-- Umami Analytics -->

<!-- Disqus Comments -->

<!-- utterances Comments -->

</body>
</html>
